package com.atguigu.lease.web.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.entity.ViewAppointment;
import com.atguigu.lease.model.enums.BaseEnum;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.LabelInfoMapper;
import com.atguigu.lease.web.app.mapper.RoomInfoMapper;
import com.atguigu.lease.web.app.mapper.ViewAppointmentMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@RequiredArgsConstructor
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {
    private final ViewAppointmentMapper viewAppointmentMapper;
    private final ApartmentInfoService apartmentInfoService;
    private final GraphInfoService graphInfoService;
    private final LabelInfoService labelInfoService;
    private final RoomInfoService roomInfoService;
    private final LabelInfoMapper labelInfoMapper;
    private final RoomInfoMapper roomInfoMapper;
    @Override
    public List<AppointmentItemVo> getAppointmentItemVo(Long userId) {
        return viewAppointmentMapper.getAppointmentItemVo(userId);//i
        //return viewAppointmentMapper.listItemByUserId(userId);//teacher
    }

    @Override
    public AppointmentDetailVo getAppointmentDetailVo(Long id) {
        AppointmentDetailVo vo=new AppointmentDetailVo();
        //预约信息
        ViewAppointment viewAppointment = getById(id);
        BeanUtils.copyProperties(viewAppointment,vo);
        //公寓基本信息
        ApartmentItemVo apartmentItemVo=new ApartmentItemVo();
        Long apartmentId = viewAppointment.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(apartmentId);
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        //获取图片信息
        List<GraphInfo> graphInfos = graphInfoService.lambdaQuery()
                .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getItemId, apartmentInfo.getId())
                .list();
        List<GraphVo> graphVos = BeanUtil.copyToList(graphInfos, GraphVo.class);
        apartmentItemVo.setGraphVoList(graphVos);
        //获取标签信息
        List<LabelInfo> labelInfos = labelInfoMapper.selectListByApartmentId(apartmentInfo.getId());
        apartmentItemVo.setLabelInfoList(labelInfos);

        //获取最小租金
        BigDecimal minRent = roomInfoMapper.getMinRent(apartmentInfo.getId());
        apartmentItemVo.setMinRent(minRent);

        vo.setApartmentItemVo(apartmentItemVo);
        return vo;
    }
}




